<?php
//
/*
 * date:2012-0602 
 * author:bennet 
 * 妆品库 action
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ZpkuAction extends CommonAction
{
	public function index()
	{
            $where = '';
            $parameter = array();
            $zp_name = trim($_REQUEST['zp_name']);

            if(!empty($zp_name))
            {
                    $this->assign("zp_name",$zp_name);
                    $parameter['zp_name'] = $zp_name;
                    $where .= " AND zp_name LIKE '%".mysqlLikeQuote($zp_name)."%'";
            }

            $model = M();
            
            if(!empty($where))
                    $where = 'WHERE 1' . $where;

            $sql = 'SELECT COUNT(zp_id) AS tcount
                    FROM fanwe_zpku '.$where;

            $count = $model->query($sql);
            $count = $count[0]['tcount'];

            $sql = 'SELECT * FROM fanwe_zpku '.$where;
            $this->_sqlList($model,$sql,$count,$parameter,'zp_id');
            $this->display();
        }	
        
        public function remove()
	{
            Vendor('common');
            //删除指定记录
            $result = array('isErr'=>0,'content'=>'');
            $id = $_REQUEST['id'];
            if(!empty($id))
            {
                    $model = D("Zpku");


                    $list = FDB::fetchAll("SELECT zp_pic FROM ".FDB::table("Zpku")." WHERE zp_id in ($id)");
                    if($list)
                    {
                            foreach($list as $k => $v)
                            {                                    
                                    @unlink(FANWE_ROOT.$v['zp_pic']);                                    
                            }
                    }

                    $condition = array('zp_id' => array('in',explode (',',$id)));
                    if(false !== $model->where ( $condition )->delete())
                    {
                            D("zpku")->where($condition)->delete();
                            $this->saveLog(1,$id);
                    }
                    else
                    {
                            $result['isErr'] = 1;
                            $result['content'] = L('REMOVE_ERROR');
                    }
            }
            else
            {
                    $result['isErr'] = 1;
                    $result['content'] = L('ACCESS_DENIED');
            }

            die(json_encode($result));
	}
        public function add()
	{
            $model = M();
            
            $parameter = array();
      
            
            /*$where = ' WHERE cate_id = 42 and is_hot = 1';
            
            $sql = 'SELECT COUNT(gct.tag_id) AS tcount
			FROM '.C("DB_PREFIX").'goods_category_tags as gct 
				LEFT JOIN '.C("DB_PREFIX").'goods_tags as gt ON gt.tag_id = gct.tag_id '.$where;

            $count = $model->q/uery($sql);
            $count = $count[0]['tcount'];
            $count = 10;*/
            //zp_pingpai
            $sql = 'SELECT gct . * , gt.tag_name
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE cate_id =42
                    ';
            $q_list = $model->query($sql);
            //$this->_sqlList($model,$sql,$count,$parameter,'tag_id');
            //$list = $this->get('list');
            $this->assign('q_list',$q_list);
            
            $sql = 'SELECT gct.* , gt.tag_name
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE cate_id =44                    
                    ';
            $g_list = $model->query($sql);
            $this->assign('g_list',$g_list);
            
            
            $sql = 'SELECT gct.* , gt.tag_name
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE cate_id = 11                   
                    ';
            
            $ot_list = $model->query($sql);
            $this->assign('ot_list',$ot_list);

            $sql = 'SELECT gct.* , gt.tag_name
                FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                WHERE cate_id = 12                    
                ';
            $ml_list = $model->query($sql);
            $this->assign('ml_list',$ml_list);
            
            
            $sql = 'SELECT gct.* , gt.tag_name
                FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                WHERE cate_id = 13                    
                ';
            $my_list = $model->query($sql);
            $this->assign('my_list',$my_list);
            
            $this->display();

        }
        function edit()
	{
            Vendor('common');
            $model = D ("Zpku");
            
            $parameter = array();

            $id = $_REQUEST ['zp_id'];
            
            $vo = D ("Zpku")->getById ( $id );
            if(!$vo)
            {
                    $this->error(L("NO_SHARE"));
            }
            
            //妆品品牌radio_id 获取
            $sql = 'SELECT tag_id as tt_id
                    FROM '.C("DB_PREFIX").'goods_tags
                    WHERE tag_name ="'.$vo['zp_pingpai'].'"';
            $pp_id = $model->query($sql); 
            $pp_id = $pp_id[0]['tt_id'];
            $this->assign('pp_id',$pp_id);
           
            //妆品功效的checkbox_id 获取
            $gx_arr = explode(" ",$vo['zp_gongxiao']);
            $count = count($gx_arr);
            for($i = 0; $i < $count; $i++)
            {
                $sql = 'SELECT tag_id as tt_id
                    FROM '.C("DB_PREFIX").'goods_tags
                    WHERE tag_name ="'.$gx_arr[$i].'"';
                $tmp_id = $model->query($sql); 
                $gx_id[$i] = $tmp_id[0]['tt_id'];
                
            }
            $this->assign('gx_id',$gx_id);
            //$this->error ($gx_id[0]); 用来测试变量
            
            //妆品属性select_id获取
            //tag_id
            $sql = 'SELECT tag_id as tt_id
                    FROM '.C("DB_PREFIX").'goods_tags
                    WHERE tag_name ="'.$vo['zp_biaoqian'].'"';
            $bq_id = $model->query($sql); 
            $bq_id = $bq_id[0]['tt_id'];
            
            //tagid所属的分类id
            $sql = 'SELECT gct.cate_id as ca_id
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE gct.tag_id ='.$bq_id;
            $bq_cate_id = $model->query($sql); 
            $bq_cate_id = $bq_cate_id[0]['ca_id'];
            
            
            $this->assign('bq_id',$bq_id);
            $this->assign('bq_cate_id',$bq_cate_id);
            
            $this->assign ( 'vo', $vo );
            
            
            //下面显示各分类tag
            //42为品牌名称
            $sql = 'SELECT gct . * , gt.tag_name
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE cate_id =42
                    ';
            $q_list = $model->query($sql);            
            $this->assign('q_list',$q_list);
            
            //44为妆品功效
            
            $sql = 'SELECT gct.* , gt.tag_name
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE cate_id =44                    
                    ';
            $g_list = $model->query($sql);
            $this->assign('g_list',$g_list);
            
            
            //11：其他美妆，12：魅力彩妆，13：美颜护肤
            $sql = 'SELECT gct.* , gt.tag_name
                    FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                    LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                    WHERE cate_id = 11                   
                    ';            
            $ot_list = $model->query($sql);
            $this->assign('ot_list',$ot_list);

            $sql = 'SELECT gct.* , gt.tag_name
                FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                WHERE cate_id = 12                    
                ';
            $ml_list = $model->query($sql);
            $this->assign('ml_list',$ml_list);
            
            
            $sql = 'SELECT gct.* , gt.tag_name
                FROM '.C("DB_PREFIX").'goods_category_tags AS gct
                LEFT JOIN '.C("DB_PREFIX").'goods_tags AS gt ON gt.tag_id = gct.tag_id
                WHERE cate_id = 13                    
                ';
            $my_list = $model->query($sql);
            $this->assign('my_list',$my_list);
            
            $this->display();
            
        }
        
        public function update()
	{
		$id = intval($_REQUEST['zp_id']);
		$zp_gx = $_POST['zp_gongxiao'];
		
		$count = count($_POST['zp_gongxiao']);
                for($i = 0; $i < $count; $i++)
                    $zp_gx_ac .= $zp_gx[$i].' ';

                
		$name=$this->getActionName();
		$model = D( $name );
		if (false === $data = $model->create()) {
			$this->error ( $model->getError () );
		}
		$data['zp_gongxiao']=$zp_gx_ac;
		// 更新数据
		$list=$model->save($data);
		if (false !== $list)
		{
			if($upload_list = $this->uploadImages())
			{
				$cate_icon = $upload_list[0]['recpath'].$upload_list[0]['savename'];
				$model->where('zp_id = '.$id)->setField('zp_pic',$cate_icon);
			}
			$this->saveLog(1,$id);
			$this->assign('jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('EDIT_SUCCESS'));
		}
		else
		{
			//错误提示
			$this->saveLog(0,$id);
			$this->error (L('EDIT_ERROR'));
		}
	}
        
        public function insert()
        {
                //$_POST['is_root'] = isset($_POST['is_root']) ? intval($_POST['is_root']) : 0;
		//$_POST['is_index'] = isset($_POST['is_index']) ? intval($_POST['is_index']) : 0;
		$zp_gx = array();
                $zp_gx = $_POST['zp_gongxiao'];
                
		$model = D("Zpku");
		if(false === $data = $model->create())
		{
			$this->error($model->getError());
		}
		
               // $zp_gx_ac = ' '; 
                $count = count($_POST['zp_gongxiao']);
                for($i = 0; $i < $count; $i++)
                    $zp_gx_ac .= $zp_gx[$i].' ';

                $data['zp_gongxiao']=$zp_gx_ac;
		//保存当前数据对象
		$list=D("Zpku")->add($data);
		if ($list !== false)
		{
			if($_POST['is_root'] == 1)
				D("Zpku")->where('zp_id <> '.$list)->setField('is_root',0);
				
			if($upload_list = $this->uploadImages())
			{
				$cate_icon = $upload_list[0]['recpath'].$upload_list[0]['savename'];
				D("Zpku")->where('zp_id = '.$list)->setField('zp_pic',$cate_icon);
			}
			$this->saveLog(1,$list);
			$this->success (L('ADD_SUCCESS'));

		}
		else
		{
			$this->saveLog(0,$list);
			$this->error (L('ADD_ERROR'));
		}
        }
        
        
        public function removeZppic()
        {
            $photo_id = intval($_REQUEST['zp_id']);
            $photo_data = D("Zpku")->where("zp_id=".$photo_id)->find(); 
            
            
            //$this->error (L($photo_data['zp_id']));
            
            
            Vendor('common');
            if($photo_data['zp_id'])
            {
                    $count = D("Zpku")->where("zp_id=".$photo_id)->count();
                    if($count)
                            deleteShareImg($photo_data['zp_pic'],$photo_data['server_code']);
            }

            D("Zpku")->where("zp_id=".$photo_id)->setField("zp_pic",NULL);
            $err = D()->getDbError();
            if($err)
            {
                    $result['isErr'] = 1;
                    $result['content'] = $err;
            }
            else
            {
                    $result['isErr'] = 0;
            }

            
            die(json_encode($result));
        }
 
}
?>
